Example: Object-Oriented Programming
Product: CODESYS Development System
This example shows how object-oriented programming can be used. It shows the implementation of a building with different rooms. A visualization is used for better illustration.
![]() |
Description
This example shows a building with three different types of rooms. The first type has only one light. The second one has an additional light, while the third one has two lights and additionally displays the temperature. During the day, all lights are on and the temperature is set to 25 °C. At night, all lights are off and the temperature is set at 15 °C. In order to show the conversion from an interface to a function block, the temperature of room six is always set to 35 °C.
Additional information
ILight
: Interface to set and get the light valueIRoom
: interface to performing some operations when the daytime or nighttime occursITemp
: Interface to set and get the temperatureRoomType1
: This function block implements theILight
andIRoom
interfaces. Therefore, all methods of the interfaces have to be implemented. TheOperation_Daytime
function turns the light on andOperation_Nighttime
turns the light off.RoomType2
: This function block extendsRoomType1
and inherits all methods. TheSetLight
method is overridden and extended for the additional light.RoomType3
: This function block extendsRoomType2
and additionally implements theITemp
interface. TheOperation_Daytime
andOperation_Nighttime
methods are overridden and extended with the temperature.CheckFB
: Checks the time of day. Each time period lasts five seconds.BuildingManager
: Depending on the time of day, the corresponding operations are performed. In addition, the__QUERYINTERFACE
function performs a conversion from theITemp
interface to the function block ofRoomType3
.Visualization
: Shows eight rooms of three different types. The light bulbs are turned on during the day and turned off at night. In two rooms, the temperature is also displayed. This value determines the size and color of the bar.
System requirements and restrictions
Programming system | CODESYS Development System (version 3.5.14.0 or higher) |
Runtime system | CODESYS Control Win (version 3.5.14.0) |
Add-on components | - |
Note
DOWNLOAD Project